<template>
  <a-popover position="left" v-model:popup-visible="visiable" trigger="click">
    <span class="icon icon-paixu cursor" style="font-size: 20px"></span>
    <template #content>
      <div class="container">
        <a-scrollbar style="max-height: 200px; overflow: auto">
          <div>
            <div class="mb5" v-for="(item, index) of resultArr" :key="index">
              <a-space>
                <a-select v-model="item.sortField" placeholder="请选择" style="width: 180px">
                  <a-option :value="item.key" v-for="(item, index) of list" :key="index">{{ item.name }}</a-option>
                </a-select>

                <div class="right-sel clearfix">
                  <div @click="item.sortAsc = true" :class="[item.sortAsc ? 'active' : '']" class="fl item">顺序</div>
                  <div @click="item.sortAsc = false" :class="[!item.sortAsc ? 'active' : '']" class="fl item">倒序</div>
                </div>

                <!-- <icon-plus-circle style="font-size: 26px" /> -->
                <icon-delete v-if="index > 0" style="font-size: 26px" @click="resultArr.splice(index, 1)" />
              </a-space>
            </div>
          </div>
        </a-scrollbar>
      </div>
      <div class="footer clearfix">
        <div class="fl">
          <a-button type="text" @click="add">
            <icon-plus />
            <span>排序条件</span>
          </a-button>
        </div>
        <div class="fr">
          <a-button size="mini" class="gray-outline mr10" @click="visiable = false">取消</a-button>
          <a-button size="mini" type="primary" @click="submit">应用</a-button>
        </div>
      </div>
    </template>
  </a-popover>
</template>

<script lang="ts" setup>
  import { onMounted, ref } from 'vue'
  const props = defineProps(['list'])
  const emits = defineEmits(['save'])
  let visiable = ref(false)

  let resultArr = ref([{ sortField: '', sortAsc: true }])

  const add = () => {
    let obj = { sortField: '', sortAsc: true }
    resultArr.value.push(obj)
  }

  const submit = () => {
    let newArr = resultArr.value.reduce((pre: any, cur: any) => {
      let index = pre.findIndex((e: any) => cur.sortField == e.sortField)
      if (index == -1 && cur.sortField) {
        pre.push(cur)
      }
      return pre
    }, [])
    emits('save', newArr)
    visiable.value = false
  }
</script>

<style lang="less" scoped>
  .footer {
    text-align: right;
    margin-top: 5px;
  }

  .container {
    width: 340px;
    height: 200px;
  }

  .right-sel {
    padding: 2px;
    border: 1px solid #eaeaea;
    .item {
      width: 50px;
      height: 26px;
      text-align: center;
      line-height: 26px;
      font-size: 12px;
      cursor: pointer;
    }

    .active {
      background: #dfdfdf;
      border-radius: 3px;
    }
  }
</style>
